﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Threading;
using NoSmtpSender;
using Util;
namespace b2cServer
{
    public partial class ExeMail : Office2007Form
    {
        public ExeMail()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SqlHelper sh = new SqlHelper();

            List<Util.Email> list = sh.GetEmailById(txtType.Text);

            if (list == null || list.Count < 1)
            { return; }

            for (int i = 0; i < list.Count; i++)
            {

                ListViewItem item = new ListViewItem(list[i].ID.ToString());
                string[] strSubItems = { list[i].Type, list[i].Mail, list[i].Address, list[i].Datatime };
                item.SubItems.AddRange(strSubItems);
                item.ForeColor = Color.Blue;
                lstDetail.Items.Add(item);
            }

        }


        private void Run()
        {
            try
            {
                List<string> lst = new List<string>();
                PublicDelegate.GetListViewItems(lstDetail, ref  lst);

                for (int i = 0; i < lst.Count; i++)
                {
                    NoSmtpSender.Email email = new NoSmtpSender.Email();
                    Content.mail.to = lst[i];
                    Content.mail.toname = lst[i];
                    email.Mail = Content.mail;

                    if (!email.SendMail())
                        MessageBox.Show(email.ErrorMessage, "发送失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                        MessageBox.Show("恭喜，您的邮件已成功地到达了。", "发送完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch { MessageBox.Show("Exception", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Information); }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(this.Run));
            th.Start();
        }

        private void btnContent_Click(object sender, EventArgs e)
        {
            Content c = new Content();
            c.ShowDialog();
        }

    }
}